package com.open.rbac.service.sys.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.open.rbac.model.entity.sys.Role;


@Transactional(rollbackFor = Exception.class)
public interface RoleRepository extends JpaRepository<Role, String> {

    /**
     * 物理删除
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM role t1 WHERE t1 = ?1")
    void deleteReallyById(String id);

    List<Role> findByName(String name);

    @Query("""
        SELECT t1 FROM role t1
        INNER JOIN role_user_rel t2
        ON t1.id = t2.roleId
        WHERE t2.userId = ?1
        AND t1.enable = true
        """)
    List<Role> findByUserId(String userId);


    @Query("SELECT t1 FROM role t1 WHERE t1.id IN (?1)")
    List<Role> findByIds(List<String> ids);

    @Modifying
    @Query("UPDATE role r SET r.enable = ?2 WHERE r.id = ?1")
    void updateEnable(String id, boolean enable);

}
